package com.bwda.dsrs.filecenter.service.impl;

import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.condition.datatypewordrel.DataTypeWordRelCondition;
import com.bwda.dsrs.filecenter.domain.po.datasensitivewords.DataSensitiveWordsPo;
import com.bwda.dsrs.filecenter.domain.po.datatype.DataTypePo;
import com.bwda.dsrs.filecenter.domain.condition.datatype.DataTypeCondition;
import com.bwda.dsrs.filecenter.domain.po.datatypewordrel.DataTypeWordRelPo;
import com.bwda.dsrs.filecenter.mapper.dsrs.DataTypeMapper;
import com.bwda.dsrs.filecenter.service.DataSensitiveWordsService;
import com.bwda.dsrs.filecenter.service.DataTypeService;
import com.bwda.dsrs.filecenter.service.DataTypeWordRelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Administrator
 */
@Service
public class DataTypeServiceImpl extends BaseServiceImpl<DataTypePo, DataTypeCondition, DataTypeMapper> implements DataTypeService {

	@Autowired
	private DataTypeWordRelService dataTypeWordRelService;

	@Autowired
	private DataSensitiveWordsService dataSensitiveWordsService;

	@Override
	public List<DataTypePo> queryTreeList(DataTypeCondition condition) {
		List<DataTypePo> dataTypePoList =this.getMapper().selectList(condition);
		if (dataTypePoList.size()>0){
			queryTreeListDg(dataTypePoList);
		}
		return dataTypePoList;
	}

	private List<DataTypePo> queryTreeListDg(List<DataTypePo> list){
		list.forEach(dataTypePo -> {
			DataTypeWordRelCondition condition1 = new DataTypeWordRelCondition();
			condition1.setTypeId(dataTypePo.getTypeId());
			List<DataTypeWordRelPo> dataTypeWordRelPos = dataTypeWordRelService.queryList(condition1);
			if (dataTypeWordRelPos.size()>0){
				String boundField = "";
				for (DataTypeWordRelPo dataTypeWordRelPo:dataTypeWordRelPos){
					DataSensitiveWordsPo dataSensitiveWordsPo =dataSensitiveWordsService.query(dataTypeWordRelPo.getWordId());
					boundField = boundField+","+dataSensitiveWordsPo.getName();
				}
				boundField = boundField.substring(1);
				dataTypePo.setBoundField(boundField);
			}
			DataTypeCondition condition = new DataTypeCondition();
			condition.setParentTypeId(dataTypePo.getTypeId());
			condition.setDelFlag(BusinessConstants.NumberConstant.ZERO);
			List<DataTypePo> dataTypePoList1 =this.getMapper().selectList(condition);
			if (dataTypePoList1.size()>0){
				dataTypePo.setChildren(dataTypePoList1);
				queryTreeListDg(dataTypePoList1);
			}
		});
		return list;
	}
}